<template>
  <div class="dialog">
    <el-dialog :title="'吊水估产配置'" :visible.sync="showDialog" width="35%" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="养殖天数" class="w100p" prop="intervalDay">
          <el-input v-model="formData.intervalDay" type="number" clearable placeholder="请输入养殖天数"
            @change="formData.intervalDay < 0 ? formData.intervalDay = '' : ''" />
        </el-form-item>
        <el-form-item label="最高比例" class="w100p" prop="maxRate">
          <el-input v-model="formData.maxRate" type="number" clearable placeholder="请输入最高比例"
            @change="formData.maxRate < 0 ? formData.maxRate = '' : ''" />
        </el-form-item>
        <el-form-item label="最低比例" class="w100p" prop="minRate">
          <el-input v-model="formData.minRate" clearable placeholder="请输入最低比例"
            @change="formData.minRate < 0 ? formData.minRate = '' : ''" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import YieldConfigApi from "@/api/in-pond-estimation/hanging-breed-predict-yield-config"
import AreaApi from "@/api/basic-info-manage/area"

export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    }
  },
  data () {
    let checkMin = (rule, value, callback) => {
      if (value) {
        if (!this.formData.maxRate) {
          callback();
        } else if (Number(this.formData.minRate) < Number(this.formData.maxRate)) {
          callback();
        } else {
          console.log(Number(this.formData.minRate), Number(this.formData.maxRate), Number(this.formData.minRate) < Number(this.formData.maxRate));
          return callback(new Error("不能大于最高比例！"));
        }
      }
    };
    let checkMax = (rule, value, callback) => {
      if (value) {
        if (!this.formData.minRate) {
          callback();
        } else if (Number(this.formData.minRate) < Number(this.formData.maxRate)) {
          callback();
        } else {
          console.log(Number(this.formData.minRate), Number(this.formData.maxRate), Number(this.formData.minRate) < Number(this.formData.maxRate));
          return callback(new Error("不能小于最低比例！"));
        }
      }
    };
    return {
      formData: {},
      AreaList: [],
      rules: {
        intervalDay: [
          { required: true, message: "请输入养殖天数", trigger: ["change", "blur"] }
        ],
        maxRate: [
          { required: true, message: "请输入最高比例", trigger: ["change", "blur"] }, {
            validator: checkMax, trigger: ["blur"]
          }
        ],
        minRate: [
          { required: true, message: "请输入最低比例", trigger: ["change", "blur"] }, {
            validator: checkMin, trigger: ["blur"]
          }
        ],
      },
      showDialog: this.show
    };
  },
  computed: {
  },
  watch: {
    show (v) {
      this.showDialog = v;
    },
    nowChanceStau (v) {
      if (v === 'add') {
        this.formData = {
          staffName: sessionStorage.getItem('staffName'),
        }
        this.$nextTick(() => {
          this.$refs['ruleForm'].clearValidate()
        })
      }
      if (v === 'edit') {
        this.formData = this.editObject
      }
    }
  },
  created () {
  },
  mounted () {
    this.queryArea()
  },
  methods: {
    queryArea () {
      AreaApi.queryByCondition().then(res => {
        this.AreaList = res.data;
      })
    },
    save (formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            YieldConfigApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            YieldConfigApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close () {
      this.$emit("closeDialog");
    },
    cancel () {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped></style>
